
package handling.channel.handler;


import client.MapleCharacter;
 import client.MapleClient;
 import client.skills.Skill;
 import client.skills.SkillFactory;
 import configs.ServerConfig;
 import handling.world.WorldAllianceService;
 import handling.world.WorldGuildService;
 import handling.world.guild.MapleGuild;
 import handling.world.guild.MapleGuildCharacter;
 import handling.world.guild.MapleGuildResponse;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import server.MapleStatEffect;
 import tools.Pair;
 import tools.StringUtil;
 import tools.data.input.LittleEndianAccessor;
 import tools.packet.GuildPacket;


 public class GuildHandler {
    /*  25 */   private static final Map<String, Pair<Integer, Long>> invited = new HashMap<>();
    /*  26 */   private static final List<Integer> ApplyIDs = new ArrayList<>();
    /*  27 */   private static final ReentrantReadWriteLock applyIDsLock = new ReentrantReadWriteLock();
    /*  28 */   private static long nextPruneTime = System.currentTimeMillis() + 60000L;



    public static void addApplyIDs(int id) {
        /*  31 */
        applyIDsLock.readLock().lock();

        try {
            /*  33 */
            ApplyIDs.add(Integer.valueOf(id));

        } finally {
            /*  35 */
            applyIDsLock.readLock().unlock();

        }

    }



    public static void removeApplyIDs(int id) {
        /*  40 */
        applyIDsLock.readLock().lock();

        try {
            /*  42 */
            if (ApplyIDs.contains(Integer.valueOf(id))) {
                /*  43 */
                ApplyIDs.remove(Integer.valueOf(id));

            }

        } finally {
            /*  46 */
            applyIDsLock.readLock().unlock();

        }

    }






    public static void DenyGuildRequest(String from, MapleClient c) {
        /*  54 */
        MapleCharacter cfrom = c.getChannelServer().getPlayerStorage().getCharacterByName(from);
        /*  55 */
        if (cfrom != null && invited.remove(c.getPlayer().getName().toLowerCase()) != null) {
            /*  56 */
            cfrom.getClient().announce(GuildPacket.denyGuildInvitation(c.getPlayer().getName()));

        }

    }








    public static void GuildApply(LittleEndianAccessor slea, MapleClient c) {
        /*  66 */
        if (c.getPlayer().getGuildId() > 0) {
            /*  67 */
            c.getPlayer().dropMessage(1, "您已经有家族了，无需重复申请.");

            return;

        }
        /*  70 */
        if (ApplyIDs.contains(Integer.valueOf(c.getPlayer().getId()))) {
            /*  71 */
            c.getPlayer().dropMessage(1, "您已经在家族申请列表中，暂时无法进行此操作.");
            /*  72 */
            c.announce(MapleGuildResponse.无法申请公会.getPacket());
            /*  73 */
            removeApplyIDs(c.getPlayer().getId());

            return;

        }
        /*  76 */
        int guildId = slea.readInt();
        /*  77 */
        MapleGuildCharacter guildMember = new MapleGuildCharacter(c.getPlayer());
        /*  78 */
        guildMember.setGuildId(guildId);
        /*  79 */
        int ret = WorldGuildService.getInstance().addGuildApplyMember(guildMember);
        /*  80 */
        if (ret == 1) {
            /*  81 */
            addApplyIDs(c.getPlayer().getId());
            /*  82 */
            c.getPlayer().dropMessage(1, "您成功申请加入家族，请等待族长同意.");

        } else {
            /*  84 */
            c.getPlayer().dropMessage(1, "申请加入家族出现错误，请稍后再试.");

        }

    }








    public static void AcceptGuildApply(LittleEndianAccessor slea, MapleClient c) {
        /*  94 */
        if (c.getPlayer().getGuildId() <= 0 || c.getPlayer().getGuildRank() > 2) {

            return;

        }
        /*  97 */
        int guildId = c.getPlayer().getGuildId();
        /*  98 */
        byte amount = slea.readByte();


        /* 101 */
        int i = 0;
        while (true) {
            int fromId;
            if (i < amount)
                /* 102 */ {
                fromId = slea.readInt();
                /* 103 */
                MapleCharacter from = c.getChannelServer().getPlayerStorage().getCharacterById(fromId);

                /* 105 */
                if (from != null && from.getGuildId() <= 0)
                    /* 106 */ {
                    from.setGuildId(guildId);
                    /* 107 */
                    from.setGuildRank((byte) 5);
                    /* 108 */
                    int ret = WorldGuildService.getInstance().addGuildMember(from.getMGC());
                    /* 109 */
                    if (ret == 0)
                        /* 110 */ {
                        from.setGuildId(0);
                    }

                    else

                        /* 113 */ {
                        from.getClient().announce(GuildPacket.showGuildInfo(from));
                        /* 114 */
                        MapleGuild gs = WorldGuildService.getInstance().getGuild(guildId);
                        /* 115 */
                        for (byte[] pack : WorldAllianceService.getInstance().getAllianceInfo(gs.getAllianceId(), true)) {
                            /* 116 */
                            if (pack != null) {
                                /* 117 */
                                from.getClient().announce(pack);

                            }

                        }
                        /* 120 */
                        from.saveGuildStatus();
                        /* 121 */
                        respawnPlayer(from);

                        /* 123 */
                        if (ApplyIDs.contains(Integer.valueOf(fromId)))
                            /* 124 */ {
                            removeApplyIDs(fromId);
                            break;
                        }
                    }
                    continue;
                }
            } else {
                break;
            }
            if (ApplyIDs.contains(Integer.valueOf(fromId))) {
                removeApplyIDs(fromId);

                break;
            }


            i++;
        }


    }







    public static void DenyGuildApply(LittleEndianAccessor slea, MapleClient c) {
        /* 136 */
        if (c.getPlayer().getGuildId() <= 0 || c.getPlayer().getGuildRank() > 2) {

            return;

        }
        /* 139 */
        int guildId = c.getPlayer().getGuildId();
        /* 140 */
        byte amount = slea.readByte();

        /* 142 */
        for (int i = 0; i < amount; i++) {
            /* 143 */
            int fromId = slea.readInt();
            /* 144 */
            WorldGuildService.getInstance().denyGuildApplyMember(guildId, fromId);
            /* 145 */
            if (ApplyIDs.contains(Integer.valueOf(fromId)))
                /* 146 */ removeApplyIDs(fromId);

        }
    }

    public static void Guild(LittleEndianAccessor slea, MapleClient c) {
        int fromId;
        MapleGuild guild;
        MapleCharacter target;
        MapleGuild tmpguild;
        int cost;
        String guildName;
        int guildId;
        String name;
        MapleGuildResponse mgr;
        Pair<Integer, Long> gid;
        String[] ranks;
        int i;
        byte newRank;
        short bg;
        byte bgcolor;
        short logo;
        byte logocolor;
        String notice;
        int skillId;
        byte level;
        Skill skill;
        int newLevel;
        MapleStatEffect skillid, skillii;
        int type, subtype, minlevel;
        List<Pair<Integer, MapleGuild>> gui;
        int maxlevel;
        List<Pair<Integer, MapleGuild>> Gui;
        int minScale;

        String resguildName;

        int maxScale;

        String Patriarch;

        int minMemberLevel, maxMemberLevel;

        List<Pair<Integer, MapleGuild>> list1, list2;

        String str1;

        int Mean;
        /* 155 */
        long currentTime = System.currentTimeMillis();
        /* 156 */
        if (currentTime >= nextPruneTime) {
            /* 157 */
            Iterator<Map.Entry<String, Pair<Integer, Long>>> itr = invited.entrySet().iterator();

            /* 159 */
            while (itr.hasNext()) {
                /* 160 */
                Map.Entry<String, Pair<Integer, Long>> inv = itr.next();
                /* 161 */
                if (currentTime >= ((Long) ((Pair) inv.getValue()).right).longValue()) {
                    /* 162 */
                    itr.remove();

                }

            }
            /* 165 */
            nextPruneTime += 300000L;

        }
        /* 167 */
        MapleCharacter chr = c.getPlayer();
        /* 168 */
        byte mode = slea.readByte();
        /* 169 */
        switch (mode) {

            case 0:
                /* 171 */
                c.announce(GuildPacket.showGuildInfo(chr));

                return;

            case 1:
                /* 174 */
                fromId = slea.readInt();


                /* 177 */
                target = c.getChannelServer().getPlayerStorage().getCharacterById(fromId);
                /* 178 */
                if (target == null) {

                    /* 180 */
                    MapleGuild mapleGuild = WorldGuildService.getInstance().getGuild(fromId);
                    /* 181 */
                    if (mapleGuild == null) {
                        /* 182 */
                        chr.dropMessage(1, "找不到玩家或家族的信息.");

                        return;

                    }
                    /* 185 */
                    c.announce(GuildPacket.showPlayerGuildInfo(mapleGuild));


                    return;

                }
                /* 189 */
                if (target.getGuildId() <= 0) {
                    /* 190 */
                    chr.dropMessage(1, "玩家[" + target.getName() + "]没有家族.");


                    return;

                }
                /* 194 */
                guild = WorldGuildService.getInstance().getGuild(target.getGuildId());
                /* 195 */
                if (guild == null) {
                    /* 196 */
                    chr.dropMessage(1, "玩家[" + target.getName() + "]还没有家族.");

                    return;

                }
                /* 199 */
                c.announce(GuildPacket.showPlayerGuildInfo(guild));

                return;

            case 2:
                /* 202 */
                tmpguild = WorldGuildService.getInstance().getGuild(slea.readInt());
                /* 203 */
                if (tmpguild != null) {
                    /* 204 */
                    c.announce(GuildPacket.viewGuildInfo(tmpguild));

                }

                return;

            case 4:
                /* 208 */
                cost = ServerConfig.CHANNEL_CREATEGUILDCOST;
                /* 209 */
                if (chr.getGuildId() > 0 || chr.getMapId() != 200000301) {
                    /* 210 */
                    chr.dropMessage(1, "不能创建家族\r\n已经有家族或没在家族中心");
                    return;

                }
                /* 212 */
                if (chr.getMeso() < cost) {
                    /* 213 */
                    chr.dropMessage(1, "你没有足够的金币创建一个家族。当前创建家族需要: " + cost + " 的金币.");

                    return;

                }
                /* 216 */
                guildName = slea.readMapleAsciiString();
                /* 217 */
                if (!isGuildNameAcceptable(guildName)) {
                    /* 218 */
                    chr.dropMessage(1, "你不能使用这个名字。");

                    return;

                }
                /* 221 */
                guildId = WorldGuildService.getInstance().createGuild(chr.getId(), guildName);
                /* 222 */
                if (guildId == 0) {
                    /* 223 */
                    chr.dropMessage(1, "创建家族出错\r\n请重试一次.");

                    return;

                }
                /* 226 */
                chr.gainMeso(-cost, true, true);
                /* 227 */
                chr.setGuildId(guildId);
                /* 228 */
                chr.setGuildRank((byte) 1);
                /* 229 */
                chr.saveGuildStatus();
                /* 230 */
                chr.finishAchievement(35);
                /* 231 */
                WorldGuildService.getInstance().setGuildMemberOnline(chr.getMGC(), true, c.getChannel());
                /* 232 */
                c.announce(GuildPacket.showGuildInfo(chr));
                /* 233 */
                WorldGuildService.getInstance().gainGP(chr.getGuildId(), 500, chr.getId());
                /* 234 */
                chr.dropMessage(1, "恭喜你成功创建家族.");
                /* 235 */
                respawnPlayer(chr);

                return;

            case 7:
                /* 238 */
                if (chr.getGuildId() <= 0 || chr.getGuildRank() > 2) {

                    return;

                }
                /* 241 */
                name = slea.readMapleAsciiString().toLowerCase();
                /* 242 */
                if (invited.containsKey(name)) {
                    /* 243 */
                    chr.dropMessage(5, "玩家 " + name + " 已经在邀请的列表，请稍后在试。");

                    return;

                }
                /* 246 */
                mgr = MapleGuild.sendInvite(c, name);
                /* 247 */
                if (mgr != null) {
                    /* 248 */
                    c.announce(mgr.getPacket());

                } else {
                    /* 250 */
                    invited.put(name, new Pair(Integer.valueOf(chr.getGuildId()), Long.valueOf(currentTime + 60000L)));

                }

                return;

            case 54:
                /* 254 */
                if (chr.getGuildId() > 0) {

                    return;

                }
                /* 257 */
                guildId = slea.readInt();
                /* 258 */
                fromId = slea.readInt();
                /* 259 */
                if (fromId != chr.getId()) {

                    return;

                }
                /* 262 */
                name = chr.getName().toLowerCase();
                /* 263 */
                gid = invited.remove(name);
                /* 264 */
                if (gid != null && guildId == ((Integer) gid.left).intValue()) {
                    /* 265 */
                    chr.setGuildId(guildId);
                    /* 266 */
                    chr.setGuildRank((byte) 5);
                    /* 267 */
                    int ret = WorldGuildService.getInstance().addGuildMember(chr.getMGC());
                    /* 268 */
                    if (ret == 0) {
                        /* 269 */
                        chr.dropMessage(1, "尝试加入的家族成员数已到达最高限制。");
                        /* 270 */
                        chr.setGuildId(0);

                        return;

                    }
                    /* 273 */
                    c.announce(GuildPacket.showGuildInfo(chr));
                    /* 274 */
                    MapleGuild gs = WorldGuildService.getInstance().getGuild(guildId);
                    /* 275 */
                    for (byte[] pack : WorldAllianceService.getInstance().getAllianceInfo(gs.getAllianceId(), true)) {
                        /* 276 */
                        if (pack != null) {
                            /* 277 */
                            c.announce(pack);

                        }

                    }
                    /* 280 */
                    chr.saveGuildStatus();
                    /* 281 */
                    respawnPlayer(c.getPlayer());

                }

                return;

            case 11:
                /* 285 */
                fromId = slea.readInt();
                /* 286 */
                name = slea.readMapleAsciiString();
                /* 287 */
                if (fromId != chr.getId() || !name.equals(chr.getName()) || chr.getGuildId() <= 0) {

                    return;

                }
                /* 290 */
                WorldGuildService.getInstance().leaveGuild(chr.getMGC());
                /* 291 */
                c.announce(GuildPacket.showGuildInfo(null));

                return;

            case 12:
                /* 294 */
                fromId = slea.readInt();
                /* 295 */
                name = slea.readMapleAsciiString();
                /* 296 */
                if (chr.getGuildRank() > 2 || chr.getGuildId() <= 0) {

                    return;

                }
                /* 299 */
                WorldGuildService.getInstance().expelMember(chr.getMGC(), name, fromId);

                return;

            case 18:
                /* 302 */
                if (chr.getGuildId() <= 0 || chr.getGuildRank() != 1) {

                    return;

                }
                /* 305 */
                ranks = new String[5];
                /* 306 */
                for (i = 0; i < 5; i++) {
                    /* 307 */
                    ranks[i] = slea.readMapleAsciiString();

                }
                /* 309 */
                WorldGuildService.getInstance().changeRankTitle(chr.getGuildId(), ranks);

                return;

            case 19:
                /* 312 */
                fromId = slea.readInt();
                /* 313 */
                newRank = slea.readByte();
                /* 314 */
                if (newRank <= 1 || newRank > 5 || chr.getGuildRank() > 2 || (newRank <= 2 && chr.getGuildRank() != 1) || chr.getGuildId() <= 0) {

                    return;

                }
                /* 317 */
                WorldGuildService.getInstance().changeRank(chr.getGuildId(), fromId, newRank);

                return;

            case 20:
                /* 320 */
                if (chr.getGuildId() <= 0 || chr.getGuildRank() != 1) {

                    return;

                }
                /* 323 */
                if (chr.getMeso() < 1500000L) {
                    /* 324 */
                    chr.dropMessage(1, "金币不足 1500000。");

                    return;

                }
                /* 327 */
                bg = slea.readShort();
                /* 328 */
                bgcolor = slea.readByte();
                /* 329 */
                logo = slea.readShort();
                /* 330 */
                logocolor = slea.readByte();
                /* 331 */
                WorldGuildService.getInstance().setGuildEmblem(chr.getGuildId(), bg, bgcolor, logo, logocolor);
                /* 332 */
                chr.gainMeso(-1500000L, true, true);
                /* 333 */
                respawnPlayer(c.getPlayer());

                return;

            case 49:
                /* 336 */
                notice = slea.readMapleAsciiString();
                /* 337 */
                if (notice.length() > 100 || chr.getGuildId() <= 0 || chr.getGuildRank() > 2) {

                    return;

                }
                /* 340 */
                WorldGuildService.getInstance().setGuildNotice(chr.getGuildId(), notice);

                return;

            case 35:
                /* 343 */
                skillId = slea.readInt();
                /* 344 */
                level = slea.readByte();
                /* 345 */
                if (skillId > 0) {
                    /* 346 */
                    chr.dropMessage(1, "当前暂不支持家族技能升级.");

                    return;

                }
                /* 349 */
                skill = SkillFactory.getSkill(skillId);
                /* 350 */
                if (chr.getGuildId() <= 0 || skill == null || skill.getId() < 91000000) {

                    return;

                }

                /* 354 */
                newLevel = WorldGuildService.getInstance().getSkillLevel(chr.getGuildId(), skill.getId()) + level;
                /* 355 */
                if (newLevel > skill.getMaxLevel()) {

                    return;

                }
                /* 358 */
                skillid = skill.getEffect(newLevel);
                /* 359 */
                if (skillid.getReqGuildLevel() <= 0 || chr.getMeso() < skillid.getPrice()) {

                    return;

                }
                /* 362 */
                if (WorldGuildService.getInstance().purchaseSkill(chr.getGuildId(), skillid.getSourceid(), chr.getName(), chr.getId())) {
                    /* 363 */
                    chr.gainMeso(-skillid.getPrice(), true);

                }

                return;

            case 62:
                /* 367 */
                skill = SkillFactory.getSkill(slea.readInt());
                /* 368 */
                if (c.getPlayer().getGuildId() <= 0 || skill == null) {

                    return;

                }
                /* 371 */
                newLevel = WorldGuildService.getInstance().getSkillLevel(chr.getGuildId(), skill.getId());
                /* 372 */
                if (newLevel <= 0) {

                    return;

                }
                /* 375 */
                skillii = skill.getEffect(newLevel);
                /* 376 */
                if (skillii.getReqGuildLevel() < 0 || chr.getMeso() < skillii.getExtendPrice()) {

                    return;

                }
                /* 379 */
                if (WorldGuildService.getInstance().activateSkill(chr.getGuildId(), skillii.getSourceid(), chr.getName())) {
                    /* 380 */
                    chr.gainMeso(-skillii.getExtendPrice(), true);

                }

                return;

            case 40:
                /* 384 */
                fromId = slea.readInt();
                /* 385 */
                if (chr.getGuildId() <= 0 || chr.getGuildRank() > 1) {

                    return;

                }
                /* 388 */
                WorldGuildService.getInstance().setGuildLeader(chr.getGuildId(), fromId);

                return;

            case 44:
                /* 391 */
                if (chr.getGuildId() <= 0) {

                    return;

                }
                /* 394 */
                c.announce(GuildPacket.showGuildBeginnerSkill());

                return;

            case 45:
                /* 397 */
                type = slea.readByte();
                /* 398 */
                switch (type) {

                    case 0:
                        /* 400 */
                        subtype = slea.readByte();
                        /* 401 */
                        slea.skip(2);
                        /* 402 */
                        gui = WorldGuildService.getInstance().getGuildList();
                        /* 403 */
                        Gui = new ArrayList<>();
                        /* 404 */
                        resguildName = slea.readMapleAsciiString().toLowerCase();
                        /* 405 */
                        Patriarch = "找不到家族";

                        /* 407 */
                        for (Pair<Integer, MapleGuild> g : gui) {
                            /* 408 */
                            MapleCharacter leaderObj = ((MapleGuild) g.getRight()).getLeader(c);
                            /* 409 */
                            String gname = ((MapleGuild) g.getRight()).getName().toLowerCase();

                            /* 411 */
                            if (resguildName.equals("可用家族,公会名或族长名字搜索")) {

                                /* 413 */
                                MapleCharacter targetLeader = c.getChannelServer().getPlayerStorage().getCharacterById(((MapleGuild) g.getRight()).getLeaderId());

                                /* 415 */
                                if (targetLeader == null) {

                                    continue;

                                }
                                /* 418 */
                                Gui.add(g);
                                continue;

                            }
                            /* 420 */
                            if (((subtype == 1 || subtype == 2) && gname.contains(resguildName)) || (subtype == 1 && gname.contains(resguildName)) || (subtype == 2 && leaderObj != null && leaderObj.getName().toLowerCase().contains(resguildName))) {
                                /* 421 */
                                for (MapleGuildCharacter gchr : ((MapleGuild) g.getRight()).getMembers()) {
                                    /* 422 */
                                    if (gchr.getGuildRank() == 1) {
                                        /* 423 */
                                        Patriarch = gchr.getName();

                                    }

                                }

                                /* 427 */
                                Gui.add(g);

                            }

                        }

                        /* 431 */
                        c.announce(GuildPacket.guildSearch_Results(c, Gui));

                        break;


                    case 1:
                        /* 435 */
                        minlevel = slea.readByteAsInt();
                        /* 436 */
                        maxlevel = slea.readByteAsInt();
                        /* 437 */
                        minScale = slea.readByteAsInt();
                        /* 438 */
                        maxScale = slea.readByteAsInt();
                        /* 439 */
                        minMemberLevel = slea.readByteAsInt();
                        /* 440 */
                        maxMemberLevel = slea.readByteAsInt();

                        /* 442 */
                        list1 = WorldGuildService.getInstance().getGuildList();
                        /* 443 */
                        list2 = new ArrayList<>();
                        /* 444 */
                        str1 = "找不到族长";
                        /* 445 */
                        Mean = 0;
                        /* 446 */
                        for (Pair<Integer, MapleGuild> g : list1) {
                            /* 447 */
                            if (((MapleGuild) g.getRight()).getLevel() >= minlevel && ((MapleGuild) g.getRight()).getLevel() <= maxlevel && (
                                    /* 448 */                 (MapleGuild) g.getRight()).getCapacity() >= minScale && ((MapleGuild) g.getRight()).getCapacity() <= maxScale) {
                                /* 449 */
                                boolean isOout = false;
                                /* 450 */
                                for (MapleGuildCharacter gchr : ((MapleGuild) g.getRight()).getMembers()) {
                                    /* 451 */
                                    if (gchr.getLevel() >= minMemberLevel && gchr.getLevel() <= maxMemberLevel) {
                                        /* 452 */
                                        isOout = true;

                                    }
                                    /* 454 */
                                    if (isOout &&
                                            /* 455 */                     gchr.getGuildRank() == 1) {
                                        /* 456 */
                                        str1 = gchr.getName();

                                    }

                                }

                                /* 460 */
                                if (isOout) {
                                    /* 461 */
                                    Mean = ((MapleGuild) g.getRight()).getMembers().size() / ((MapleGuild) g.getRight()).getCapacity();
                                    /* 462 */
                                    list2.add(g);

                                }

                            }

                        }

                        /* 467 */
                        c.announce(GuildPacket.guildSearch_Results(c, list2));

                        break;

                }


                return;

        }

        /* 474 */
        System.out.println("未知家族操作类型: ( 0x" + StringUtil.getLeftPaddedStr(Integer.toHexString(mode).toUpperCase(), '0', 2) + " )" + slea.toString());
    }






    private static boolean isGuildNameAcceptable(String name) {
        /* 480 */
        return ((name.getBytes()).length >= 3 && (name.getBytes()).length <= 12);

    }



    private static void respawnPlayer(MapleCharacter chr) {
        /* 484 */
        if (chr.getMap() == null) {

            return;

        }
        /* 487 */
        chr.getMap().broadcastMessage(GuildPacket.loadGuildName(chr));
        /* 488 */
        chr.getMap().broadcastMessage(GuildPacket.loadGuildIcon(chr));

    }

}